import {ref} from "vue";
import axios from "@/utils/axios";
import {message} from "ant-design-vue";
import qs from "qs";

class TampNorm{
    cellId = -1
    energyCover:number|undefined
    energyPointFirst:number|undefined
    energyPointSecond:number|undefined
    timesCover:number|undefined
    timesPointFirst:number|undefined
    timesPointSecond:number|undefined
    gtmCreate:string|undefined
}

class TampCoordinateForm{
    cellId:number = -1
    count:number = 0
    first = new Array<number>()
    second = new Array<number>()
}

class TampDesignPointForm{
    tampDesignPoint:number|undefined
}

export default ()=>{
    const tampNormFormRef = ref()
    const tampNormFormState = ref<TampNorm>(new TampNorm())
    const tampNormRules = {}
    const tampNorm = ref({})

    const tampCoordinateTypeFormRef = ref()
    const tampCoordinateTypeFormState = ref(new TampCoordinateForm())
    const tampCoordinateTypeFormRules = {}
    const tampBorder:any = ref([])

    const tampDesignPointFormRef = ref()
    const tampDesignPointFormState = ref(new TampDesignPointForm())
    const tampDesignPointFormRules = {}
    const tampDesignPoint = ref({})

    // 获取标准
    const getTampNorm = (cellId:number)=>{
        axios.post("/tamp/findCellParameterByCellId",
            {cellId: cellId}).then((res:any)=>{
            if(res.success){
                tampNormFormState.value = new TampNorm()
                Object.assign(tampNormFormState.value,res.data)
            }else{
                message.info(res.msg)
            }
        })
    }
    // 设置标准
    const setTampNorm = (cellId:number)=>{
        tampNormFormState.value.cellId = cellId
        tampNormFormState.value.gtmCreate = undefined
        axios.post("/tamp/saveCellParameter",
            tampNormFormState.value).then((res:any)=>{
            if(res.success){
                message.info("保存成功")
            }else{
                message.info(res.msg)
            }
        })
    }
    // 获取边界
    const getTampBorder = (type:number = 1,cellId:number,callback:Function|null)=>{
        let url = type==1?"common/findCellBorderByCellIdAB":"/common/findCellBorderByCellId"
        axios.post(url,
            {cellId:cellId}).then((res:any)=>{
            if(res.success){
                tampBorder.value = new Array()
                for (let i = 0; i < res.data.borderCount; i++) {
                    tampBorder.value.push({
                        lon: res.data.first[i],
                        lat: res.data.second[i],
                    });
                }
                if(callback) callback(res)
            }else{
                message.info(res.msg)
            }
        })
    }
    // 设置边界
    const setTampBorder = (type:number = 1,callback:Function|null=null)=>{
        let url = type==1?"common/updateCellBorderByCellIdAB":"/common/updateCellBorderByCellId"
        axios.post(url,
            tampCoordinateTypeFormState.value).then((res1:any)=>{
            if(res1.success){
                createTiledMap(tampCoordinateTypeFormState.value.cellId,1).then((res2:any)=>{
                    axios.post("/qhMap/reCreatReport",
                        qs.stringify({
                            cellID:tampCoordinateTypeFormState.value.cellId,
                            workType:1,
                        }),{
                            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                        })
                    axios.post("/qhMap/reCreatReport",
                        qs.stringify({
                            cellID:tampCoordinateTypeFormState.value.cellId,
                            workType:2,
                        }),{
                            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                        })
                    axios.post("/qhMap/reCreatReport",
                        qs.stringify({
                            cellID:tampCoordinateTypeFormState.value.cellId,
                            workType:11,
                        }),{
                            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                        })
                    if(res2&&callback) callback(res1)
                })
                message.info("导入成功")
            }else{
                message.info(res1.msg)
            }
        })
    }

    const buildTampDesignPoint = (cellId:number,type:number)=>{
        axios.post("/tamp/buildDesignPoint",
            {
                cellId:cellId,
                distance:tampDesignPointFormState.value.tampDesignPoint,
                kind:type
            }).then((res:any)=>{
            if(res.success){
                createTiledMap(cellId,1)
                message.info("生成成功")
            }else{
                message.info(res.msg)
            }
        })
    }

    const setTampDesignPoint = (designData:any)=>{
        axios.post("/tamp/saveDesignPoint",designData).then((res:any)=>{
            if(res.success){
                message.info("导入成功")
            }else{
                message.info(res.msg)
            }
        })
    }

    const createTiledMap = (cellId:number,type:number)=>{
        return new Promise((resolve,reject)=>{
            axios.post("/qhMap/reCreatBorder",
                qs.stringify({
                    cellID:cellId,
                    workType:type,
                }),{
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                }).then((res:any)=>{
                    resolve(res.success)
            })
        })
    }

    return{
        tampNormFormRef,
        tampNormFormState,
        tampNormRules,
        tampNorm,

        tampCoordinateTypeFormRef,
        tampCoordinateTypeFormState,
        tampCoordinateTypeFormRules,
        tampBorder,

        tampDesignPointFormRef,
        tampDesignPointFormState,
        tampDesignPointFormRules,
        tampDesignPoint,

        getTampNorm,
        setTampNorm,
        getTampBorder,
        setTampBorder,
        buildTampDesignPoint,
        setTampDesignPoint,
        createTiledMap
    }

}